﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Text;
using System.Configuration;
using System.Runtime.InteropServices;

 
namespace epass.share
{
    public class Program
    {
		
        
		/// <summary>
		/// 根据登陆的用户名验证ePass中的key是否与用户的key匹配
		/// </summary>
		/// <param name="loginname">登陆账户</param>
		/// <returns>
		/// 返回查询结果-2:没有插入证书;-1:没有安装epass驱动;0:成功匹配;1验证失败,不存在;2:验证失败,系统的key已经过期
		/// </returns>
		static public string expires(string loginname,string key,string verify)
		{
			string epass=ConfigurationSettings.AppSettings.Get("epass").ToString();

			if(loginname=="admin" || epass=="false" || verify=="false") return "0";
			string returnValue="";			 
			System.Data.DataRow map=sqlRow("select * from t_key where loginname ='"+loginname+"' and [key]='"+key+"'");
			if(map!=null)
			{
				if(map["isuse"].ToString()=="0")
				{
					returnValue="2";
				}
				else
				{
					returnValue="0";
				}
			}
			else
			{
				returnValue="1";
			}
			return returnValue;
		}
		/// <summary>
		/// 执行SQL查询语句，返回第一条记录的DataRow结果。
		/// </summary>
		/// <param name="strSql">SQL语句。</param>
		/// <returns>
		/// 返回第一条记录的DataRow结果。
		/// </returns>
		static public DataRow sqlRow(string strSql)
		{
			DataSet dataSet=sqlDataSet_SQLSERVER(strSql,0,0);
			if(dataSet==null||dataSet.Tables==null||dataSet.Tables.Count==0||dataSet.Tables[0]==null||dataSet.Tables[0].Rows.Count==0||dataSet.Tables[0].Rows[0]==null) return null;
			return dataSet.Tables[0].Rows[0];
		}
		/// <summary>
		/// SQLSERVER数据库：执行SQL查询语句，起始记录idx(0..)，以及读取记录数count，返回记录的DataSet集合。
		/// </summary>
		/// <param name="strSql">SQL语句。</param>
		/// <param name="idx"> 起始记录idx</param>
		/// <param name="count"> 要读取的记录数</param>
		/// <returns>
		/// 返回记录的DataSet集合。
		/// </returns>
		static private DataSet sqlDataSet_SQLSERVER(string strSql,int idx,int count) //,out object objError)
		{
			 
			SqlConnection conn=null;SqlDataAdapter adapter=null; 
			try
			{
				string CONNECT=ConfigurationSettings.AppSettings.Get("DatabaseConnect").ToString();
				conn =new SqlConnection(CONNECT);
				conn.Open();
				adapter = new SqlDataAdapter(strSql,conn);
				adapter.SelectCommand.CommandType=CommandType.Text;
				//adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
				DataSet dataSet=new DataSet();
				if(idx<=0 && count<=0) adapter.Fill(dataSet);
				else adapter.Fill(dataSet,idx,count,"main");
				adapter.Dispose();
				conn.Close();
				return dataSet;
			}
			catch(Exception e)
			{
				if(conn!=null) conn.Close();
				if(adapter!=null) adapter.Dispose();
				 
				 
			}
			return null;
		}//end method
    }
}
